package org.example.server.app.goods.service.impl;

import jakarta.annotation.Resource;
import org.example.api.search.controller.SearchControllerFeign;
import org.example.api.search.controller.request.GoodsSearchRequest;
import org.example.api.search.controller.response.GoodsDocSummeryResponse;
import org.example.frw.api.base.response.PageResponse;
import org.example.server.app.goods.mapstruct.GoodsAppConverter;
import org.example.server.app.goods.request.GoodsSearchAppRequest;
import org.example.server.app.goods.response.goods.Goods;
import org.example.server.app.goods.service.GoodsService;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {
  @Resource
  private SearchControllerFeign searchControllerFeign;
  @Resource
  private GoodsAppConverter goodsAppConverter;

  @Override
  public PageResponse<Goods> searchGoods(GoodsSearchAppRequest searchRequest) {
    // 分页查询商品
    GoodsSearchRequest request = new GoodsSearchRequest();
    request.setCategoryId(searchRequest.getCategoryId());
    request.setKeyword(searchRequest.getKeyword());
    request.setOrderBy(searchRequest.getOrder());
    request.setDirection(searchRequest.getSort());
    request.setPage(searchRequest.getPage());

    PageResponse<GoodsDocSummeryResponse> response = searchControllerFeign.searchGoods(request);
    PageResponse.PageResponseBuilder<Goods> builder = PageResponse.builder();
    return builder
            .total(response.getTotal())
            .current(response.getCurrent())
            .pageSize(response.getPageSize())
            .items(goodsAppConverter.toGoods(response.getItems()))
            .build();
  }
}
